/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_jolt_header_unpack.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/01 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/tuxedojolt/config.h"
#include <tuxedojolt/jolt_core.h>
#include <tuxedojolt/jolt_header.h>
#include <tuxedojolt/jolt_comm.h>
#include <tuxedojolt/jolt_packet.h>
#include <apmtest_comm.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    char *p_bin = (char *) "4a4f4c540000000f00000080000000000000003200000040ffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    Jolt_header st_jolt_header;
    memset(&st_jolt_header, 0x00, sizeof(st_jolt_header));

    CU_ASSERT_EQUAL(jolt_header_unpack(arr_payload, strlen(p_bin) / 2, &st_jolt_header), FR_SUCC);
    CU_ASSERT_EQUAL(st_jolt_header.ui_version, 15);
    CU_ASSERT_EQUAL(st_jolt_header.ui_length, 128);
    CU_ASSERT_EQUAL(st_jolt_header.ui_msgcode, 0x32);
    CU_ASSERT_EQUAL(st_jolt_header.ui_session_id, 0xFFFFFFFF);
    CU_ASSERT_EQUAL(st_jolt_header.ui_handler_id, 0xFFFFFFFF);
    CU_ASSERT_EQUAL(st_jolt_header.ui_msg_id, 0);
    CU_ASSERT_EQUAL(st_jolt_header.ui_uncmprsz, 0);
}

/**
 * 异常测试
 * 包长小于通用头长度
 */
static void __case2(void)
{
    char *p_bin = (char *) "4a4f4c540000000f00000080000000000000003200000040ffffffffffffffff00000000000000000000000000000000000000000000000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    Jolt_header st_jolt_header;
    memset(&st_jolt_header, 0x00, sizeof(st_jolt_header));

    CU_ASSERT_EQUAL(jolt_header_unpack(arr_payload, strlen(p_bin) / 2, &st_jolt_header), FR_FAIL);
}

/**
 * 异常测试
 * magic标志位错误
 */
static void __case3(void)
{
    char *p_bin = (char *) "1a4f4c540000000f00000080000000000000003200000040ffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    Jolt_header st_jolt_header;
    memset(&st_jolt_header, 0x00, sizeof(st_jolt_header));

    CU_ASSERT_EQUAL(jolt_header_unpack(arr_payload, strlen(p_bin) / 2, &st_jolt_header), FR_FAIL);
}

int add_suite_jolt_header_unpack(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" jolt_header_unpack()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
